package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import components.JImagePanel;

import models.cad.Cargo;
import models.cad.Funcionario;
import models.cad.Usuario;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

import java.awt.CardLayout;

@SuppressWarnings("serial")
public class CadFunGUI extends GUIInternalFrame implements GUICadInterface{
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JTextField tfEnd;
	public JTextField tfCid;
	public JTextField tfEmail;
	public JImagePanel pnFoto;
	public JFormattedTextField tfDtNasc;
	public JFormattedTextField tfFone;
	public JFormattedTextField tfCEP;
	public JFormattedTextField tfUF;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JButton btNCrg;
	public JFormattedTextField tfCodCrg;
	public JLabel lbCargo;
	public JPanel panelFt;
	
	public CadFunGUI() {
		setTitle("Cadastro de Funcion\u00E1rios");
		setSize(new Dimension(390, 340));
		
		tfNome = new JTextField();
		tfNome.setBounds(71, 56, 275, 20);
		getContentPane().add(tfNome);
		tfNome.setColumns(10);
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(71, 25, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel lblNewLabel = new JLabel("Cod.:");
			lblNewLabel.setLabelFor(tfCod);
			lblNewLabel.setBounds(27, 28, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Nome:");
			lblNewLabel_1.setLabelFor(tfNome);
			lblNewLabel_1.setBounds(27, 59, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			JLabel lblCep = new JLabel("CEP:");
			lblCep.setBounds(27, 90, 46, 14);
			getContentPane().add(lblCep);
			
			JLabel lblEnd = new JLabel("End.:");
			lblEnd.setBounds(27, 121, 46, 14);
			getContentPane().add(lblEnd);
			
			tfEnd = new JTextField();
			lblEnd.setLabelFor(tfEnd);
			tfEnd.setColumns(10);
			tfEnd.setBounds(71, 118, 275, 20);
			getContentPane().add(tfEnd);
			
			JLabel lblEstado = new JLabel(" UF:");
			lblEstado.setBounds(150, 90, 30, 14);
			getContentPane().add(lblEstado);
			
			JLabel lblCidade = new JLabel("Cidade:");
			lblCidade.setBounds(216, 90, 46, 14);
			getContentPane().add(lblCidade);
			
			tfCid = new JTextField();
			lblCidade.setLabelFor(tfCid);
			tfCid.setColumns(10);
			tfCid.setBounds(260, 87, 86, 20);
			getContentPane().add(tfCid);
			
			tfCEP = new JFormattedTextField(new MaskFormatter("#####-###"));
			lblCep.setLabelFor(tfCEP);
			tfCEP.setColumns(3);
			tfCEP.setBounds(71, 87, 69, 20);
			getContentPane().add(tfCEP);
			
			tfUF = new JFormattedTextField(new MaskFormatter("UU"));
			lblEstado.setLabelFor(tfUF);
			tfUF.setColumns(3);
			tfUF.setBounds(184, 87, 22, 20);
			getContentPane().add(tfUF);
			
			tfEmail = new JTextField();
			tfEmail.setColumns(10);
			tfEmail.setBounds(71, 149, 145, 20);
			getContentPane().add(tfEmail);
			
			JLabel label = new JLabel("E-Mail:");
			label.setLabelFor(tfEnd);
			label.setBounds(27, 152, 46, 14);
			getContentPane().add(label);
			
			tfFone = new JFormattedTextField(new MaskFormatter("(##) ####-####"));
			tfFone.setBounds(71, 180, 96, 20);
			getContentPane().add(tfFone);
			
			JLabel lblNewLabel_2 = new JLabel("Fone.:");
			lblNewLabel_2.setLabelFor(tfFone);
			lblNewLabel_2.setBounds(27, 183, 46, 14);
			getContentPane().add(lblNewLabel_2);
			
			JLabel lblNasc = new JLabel("Nasc.:");
			lblNasc.setBounds(27, 214, 46, 14);
			getContentPane().add(lblNasc);
			
			tfDtNasc = new JFormattedTextField(new MaskFormatter("##/##/####"));
			lblNasc.setLabelFor(tfDtNasc);
			tfDtNasc.setBounds(71, 211, 96, 20);
			getContentPane().add(tfDtNasc);
			
			panelFt = new JPanel();
			panelFt.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panelFt.setBounds(260, 149, 86, 100);
			getContentPane().add(panelFt);
			panelFt.setLayout(new CardLayout(0, 0));
			
			pnFoto = new JImagePanel();
			pnFoto.add(new JLabel("Foto"));
			panelFt.add(pnFoto);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(27, 260, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			JLabel lblCargo = new JLabel("Cargo:");
			lblCargo.setBounds(119, 28, 34, 14);
			getContentPane().add(lblCargo);
			
			tfCodCrg = new JFormattedTextField(new MaskFormatter("###"));
			tfCodCrg.setColumns(3);
			tfCodCrg.setBounds(163, 25, 30, 20);
			getContentPane().add(tfCodCrg);
			
			lbCargo = new JLabel(".");
			lbCargo.setBounds(226, 28, 120, 14);
			getContentPane().add(lbCargo);
			
			btNCrg = new JButton("N");
			btNCrg.setBounds(195, 24, 23, 23);
			getContentPane().add(btNCrg);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfCodCrg.addFocusListener(fl);
        tfDtNasc.addFocusListener(fl);
        btNCrg.addActionListener(ac);
        btNCrg.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);	
        pnFoto.addMouseListener(ml);			
        panelFt.addMouseListener(ml);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfCodCrg.setText("");
		tfCEP.setText("");
		tfUF.setText("");
		tfCid.setText("");
		tfEnd.setText("");
		tfEmail.setText("");
		tfFone.setText("");
		tfDtNasc.setText("");
		lbCargo.setText("");
		pnFoto.clearImage();
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		Funcionario fun = (Funcionario) obj;
		tfCod.setText(fun.getCodigo());
		tfNome.setText(fun.getNome());
		tfCodCrg.setText(fun.getCargo().getCodigo());
		tfCEP.setText(fun.getCEP());
		tfUF.setText(fun.getEstado());
		tfCid.setText(fun.getCidade());
		tfEnd.setText(fun.getEndereco());
		tfEmail.setText(fun.getEmail());
		tfFone.setText(fun.getTelefone());
		tfDtNasc.setText(new SimpleDateFormat("dd/MM/yyyy").format(fun.getDataNasc()));
	}
	
	public Funcionario getDados(){
		Funcionario fun = new Funcionario();
		fun.setCodigo(tfCod.getText());
		fun.setNome(tfNome.getText());
		fun.setCargo(Cargo.getCargo(tfCodCrg.getText()));
		fun.setCEP(tfCEP.getText());		
		fun.setEstado(tfUF.getText());
		fun.setCidade(tfCid.getText());
		fun.setEndereco(tfEnd.getText());
		fun.setEmail(tfEmail.getText());
		fun.setTelefone(tfFone.getText());
		try {
			java.util.Date data = new SimpleDateFormat("dd/MM/yyyy").parse(tfDtNasc.getText());
			fun.setDataNasc(new java.sql.Date(data.getTime()));
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		fun.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		fun.setDtOpe(new Date(System.currentTimeMillis()));
		return fun;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}